<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.util.*,com.jspsmart.upload.*" errorPage=""%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.lang.time.DateUtils"%>
<%@page import="java.text.SimpleDateFormat"%>
<html>
	<head>
		<title>文件上传处理页面</title>
		<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
	</head>

	<body>
<%
String field=request.getParameter("field");//接收表单元素
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String path="/UpFile/"+sdf.format(new Date())+"/";//每天创建一个目录

String filename="";
try{
	String realPath=request.getSession().getServletContext().getRealPath("/"+path);
	System.out.println(realPath);
	File f=new File(realPath);
	if(!f.exists()){
		f.mkdirs();
	}
	SmartUpload su = new SmartUpload();
	su.initialize(pageContext);
	//su.setMaxFileSize(100000);
	//su.setTotalMaxFileSize(200000);
	//su.setAllowedFilesList("doc,txt");
	su.setDeniedFilesList("exe,bat,jsp,htm,html");
	su.upload();
	for (int i=0;i<su.getFiles().getCount();i++)
	{
		com.jspsmart.upload.File file = su.getFiles().getFile(i);

		if (file.isMissing()) continue;
		String ext=file.getFileExt();
		String fileName=file.getFileName();
		//long time=System.currentTimeMillis();
		//fileName=time+"~"+fileName.substring(0,fileName.indexOf("."+ext))+"."+ext;
		fileName=fileName.substring(0,fileName.indexOf("."+ext))+"."+ext;
		file.saveAs(path + fileName,su.SAVE_VIRTUAL);
		filename=path + fileName+";"+filename;//20091127 lys修改，上传文件显示顺序 //filename+=path + fileName+";";
	}
}catch(Exception e){
	filename="-1";
	e.printStackTrace();
}
if(filename.equals("-1")){
%>
 <script language="javascript">
 alert("上传错误！");
 </script>
<%
filename="";
}else{
%>
<script language="javascript">
	var targetField=parent.parent.form1.<%= field %>;
	targetField.value=targetField.value+"<%= filename %>";
	parent.parent.form1.submit();//注意:附件上传完成后设置父级的父级页面进行提交动作
</script>
<%
}
%>
</body>
</html>
